package controller.protocol;

import entity.NegoRoom;
import entity.offer.Type;

import java.util.ArrayList;
import java.util.List;

public class AlternatingMessageProtocol extends GameProtocol {

    private int amountOfAccepts;

    public AlternatingMessageProtocol(NegoRoom room){
        super(room);
        this.amountOfAccepts = 1;
    }

    @Override
    public List<Integer> nextSpeaker() {
        List<Integer> speakers = new ArrayList<>();
        updateGameStatue();
        if(isGameEnd){

        }else if(counterMessage == null){
            speakers.add(0);
        }else{
            int lastSpeaker = counterMessage.getSource();
            speakers.add((lastSpeaker + 1)%room.getOnlineClients());
        }
        return speakers;
    }

    @Override
    public boolean isGameEnd() {
        return isGameEnd;
    }

    private void updateGameStatue(){
        Type offerType = counterMessage.getBody().getType();
        switch (offerType){
            case AcceptOffer:
                amountOfAccepts++;
                break;
            case CounterOffer:
                amountOfAccepts = 1;
                break;
            case RejectOffer:

                break;
        }
        isGameEnd = amountOfAccepts>=room.getOnlineClients();
    }
}
